//
//  YCPictureView.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/22.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

class YCPictureView: UICollectionView{

    //
    var layout = UICollectionViewFlowLayout()
 
    var status:YCStatus?{
        
        didSet{
        
            self.reloadData()
        
        }
    
    
    }
    
    //这里返回配图的张数
    func calculatePictureSize() ->CGSize{
        //item的大小
        let itemSize = CGSize(width: 90, height: 90)
        //间隙
        let margin:CGFloat = 10
    
        layout.itemSize = itemSize
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
      
     let picCount = status?.pic_urls?.count ?? 0
        //没有图片就返回0
        if picCount == 0{
            return CGSizeZero
        }else if picCount == 1{
        
            return CGSize(width: 150, height: 120)
        }else if picCount == 4{
        
            let value = 2 * (itemSize.width + margin)
            
            return CGSize(width: value, height: value)
        }
        
       
        // 其他情况
        // 列数,行数计算依赖列数,column必须为Int
        let column = 3
        
        // 宽度 = 列数 * cell的宽度 + (列数 - 1) * 间距
        let width = CGFloat(column) * itemSize.width + (CGFloat(column) - 1) * margin
        
        // 行数 = (图片的数量(Int) + 列数(Int) - 1) / 列数(Int)
        /*
        1 + 3 - 1 / 3 = 1
        2 + 3 - 1 / 3 = 1
        3 + 3 - 1 / 3 = 1
        5 + 3 - 1 / 3 = 2
        6 + 3 - 1 / 3 = 2
        7 + 3 - 1 / 3 = 3
        8 + 3 - 1 / 3 = 3
        9 + 3 - 1 / 3 = 3
        */
        let row = (picCount + column - 1) / column
        
        // 高度 = 行数 * cell的高度 + (行数 - 1) * 间距
        let height = CGFloat(row) * itemSize.height + (CGFloat(row) - 1) * margin
        return CGSize(width: width, height: height)
        
    
    }
    
    
    init(){
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        registerClass(YCPictureCell.self, forCellWithReuseIdentifier: "YCPictureViewCell")
        self.dataSource = self
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

//使用扩展来实现数据源方法，更方便看
extension YCPictureView:UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return status?.pic_urls?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("YCPictureViewCell", forIndexPath: indexPath) as! YCPictureCell
    
        cell.imageURL = status?.storePictureURLs![indexPath.item]
        print("cell.imageURLcell.imageURLcell.imageURL\(cell.imageURL)")
        
        return cell
    }
}

class YCPictureCell: UICollectionViewCell{

    
    var imageURL: NSURL? {
    
        didSet{
        
            imageView.sd_setImageWithURL(imageURL)
        
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(imageView)
        imageView.ff_Fill(self)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //MARK: - 懒加载
    private lazy var imageView: UIImageView = UIImageView()
    
    

}


